package com.eliza.music.compoment.music

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.FavoriteBorder
import androidx.compose.material.icons.rounded.PlayArrow
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberAsyncImagePainter
import com.eliza.library.utils.compose.ImageBuild
import com.eliza.music.model.entity.Category
import com.eliza.music.model.entity.Playlist
import com.eliza.music.model.tmp.PlaylistList
import com.eliza.music.ui.theme.FontSize
import com.eliza.music.ui.theme.PaddingSize


@Preview(showBackground = true, showSystemUi = true, backgroundColor = 0xffffffff)
@Composable
private fun CardByRowPreview() {
    Column {
        PlaylistView.CardByRow(data = Category<Playlist>("蒙新", 1, PlaylistList))
        PlaylistView.CardByRow(data = Category<Playlist>("蒙新", 1, PlaylistList))
    }

}


@Preview(showBackground = true, showSystemUi = true, backgroundColor = 0xffffffff)
@Composable
private fun ItemInPlayList() {
    Column {
        PlaylistView.BigCard(playlist = PlaylistList.first())
    }

}


object PlaylistView {
    @Composable
    fun BigCard(modifier: Modifier = Modifier, playlist: Playlist) {
        Card(
            modifier = Modifier.then(modifier),
            onClick = { /*TODO*/ },
            colors = CardDefaults.cardColors(containerColor = Color.Transparent)
        ) {
            Box(
                modifier = Modifier
                    .aspectRatio(1f)
                    .clip(RoundedCornerShape(PaddingSize.Normal.size)),
                contentAlignment = Alignment.TopStart,
            ) {
                Image(
                    painter = rememberAsyncImagePainter(
                        model = ImageBuild.CoilImageRequest(
                            LocalContext.current,
                            playlist.icon
                        )
                    ),
                    contentScale = ContentScale.Crop,
                    modifier = Modifier.fillMaxSize(),
                    contentDescription = "",
                )
                Row(
                    modifier = Modifier.padding(PaddingSize.Normal.size),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Icon(
                        modifier = Modifier
                            .fillMaxSize(0.15f),
                        imageVector = Icons.Rounded.FavoriteBorder,
                        contentDescription = "",
                        tint = Color.White,
                    )
                    Text(
                        text = playlist.playCount.toString(),
                        fontWeight = FontWeight.W400,
                        color = Color.White,
                        fontSize = FontSize.Small.size,
                        modifier = Modifier
                            .background(
                                Color.Gray.copy(0.03f)
                            )
                    )
                }
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .align(Alignment.BottomCenter),
                    verticalAlignment = Alignment.Bottom,
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Text(
                        text = playlist.tags.joinToString("\n"),
                        fontWeight = FontWeight.W400,
                        color = Color.White,
                        fontSize = 15.sp,
                        modifier = Modifier
                            .fillMaxWidth(0.4f)
                            .padding(PaddingSize.Normal.size)
                    )
                    Icon(
                        modifier = Modifier
                            .scale(1.3f)
                            .padding(PaddingSize.Normal.size),
                        imageVector = Icons.Rounded.PlayArrow,
                        contentDescription = "",
                        tint = Color.White,
                    )
                }
            }
            Text(
                text = playlist.description,
                fontWeight = FontWeight.W700,
                color = MaterialTheme.colorScheme.primary,
                fontSize = FontSize.Normal.size,
                lineHeight = FontSize.Normal.size,
                minLines = 2,
                maxLines = 2,
                modifier = Modifier.padding(PaddingSize.Small.size)
            )
        }
    }

    @Composable
    fun CardByRow(modifier: Modifier = Modifier, data: Category<Playlist>) {
        val screenWidthDp = LocalConfiguration.current.screenWidthDp
        val padding = Modifier
            .width((screenWidthDp / 3).dp)
            .padding(PaddingSize.Small.size)
        Card(
            modifier = Modifier.padding(0.dp, 0.dp),
            colors = CardDefaults.cardColors(containerColor = Color.Transparent),
            elevation = CardDefaults.cardElevation(),
        ) {
            Text(
                text = data.title,
                fontSize = FontSize.Normal.size,
                fontWeight = FontWeight.Bold
            )
            LazyRow() {
                items(data.children) { item ->
                    BigCard(
                        modifier = padding,
                        playlist = item
                    )
                }
            }
        }

    }

}